package patrol.mobile.com.mobilepatrol.activity.office;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.dl7.recycler.adapter.BaseQuickAdapter;
import com.dl7.recycler.listener.OnRecyclerViewItemClickListener;
import com.dl7.recycler.listener.OnRecyclerViewItemLongClickListener;
import com.dl7.recycler.listener.OnRequestDataListener;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import okhttp3.Call;
import patrol.mobile.com.mobilepatrol.R;
import patrol.mobile.com.mobilepatrol.activity.CommonListActivity;
import patrol.mobile.com.mobilepatrol.adapter.AdviceAdapter;
import patrol.mobile.com.mobilepatrol.adapter.MessageAdapter;
import patrol.mobile.com.mobilepatrol.api.Urls;
import patrol.mobile.com.mobilepatrol.bean.AdviceListBean;
import patrol.mobile.com.mobilepatrol.bean.MessageBean;
import patrol.mobile.com.mobilepatrol.utils.GsonUtil;
import patrol.mobile.com.mobilepatrol.utils.net.HttpCallBack;
import patrol.mobile.com.mobilepatrol.utils.net.HttpSender;

public class AdviceActivity extends CommonListActivity {
    private int page = 1;
    private List<AdviceListBean> adviceList;
    private List<AdviceListBean> adviceAllList = new ArrayList<>();

    @Override
    protected BaseQuickAdapter initAdapter() {
        return new AdviceAdapter(this);
    }

    @Override
    protected String initTitleName() {
        return "意见建议";
    }

    @Override
    public void configAdapter() {
        super.configAdapter();
        iv_right_title.setVisibility(View.VISIBLE);
        iv_right_title.setImageResource(R.mipmap.add_white);
        iv_right_title.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(AdviceDetailActivity.class);
            }
        });

        mAdapter.setOnItemClickListener(new OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Log.i("mmmmmmmmmmmmmmmmmmmmmmm", "onItemClick: " + position);
                AdviceDetail2Activity.show(AdviceActivity.this, adviceAllList.get(position).getId());
            }
        });

        mAdapter.setOnItemLongClickListener(new OnRecyclerViewItemLongClickListener() {
            @Override
            public boolean onItemLongClick(View view, int position) {
                //长按删除
                AdviceListBean adviceListBean = adviceList.get(position);
                if (adviceListBean.getAuditFlag().equals("0")) {
                    deleteData(adviceListBean.getId());
                }
                return false;
            }
        });
        mAdapter.setRequestDataListener(new OnRequestDataListener() {
            @Override
            public void onLoadMore() {
                requestData(false);
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        requestData(true);
    }

    private void requestData(final boolean isRefresh) {
        if (isRefresh) {
            page = 1;
        } else {
            page++;
        }
        HttpSender sender = new HttpSender(this, baseUrl, Urls.ADVICE_LIST);
        HashMap<String, String> map = new HashMap<>();
        map.put("userId", current_id);
        map.put("page", page + "");
        map.put("rows", ROWS + "");
        sender.requestPost(map, new HttpCallBack(sender) {
            @Override
            public void onError(String code, Call call, Exception e) {

            }

            @Override
            public void onResponse(String code, String response) {
                JsonParser jp = new JsonParser();
                JsonObject jo = jp.parse(response).getAsJsonObject();

                JsonArray listArray = jo.getAsJsonArray("list");
                int rows = jo.get("rows").getAsInt();
                Log.i("ROWS", "onResponse: " + rows + "----" + Math.ceil(rows / ROWS));
                if (isRefresh) {
                    adviceAllList.clear();
                }
                adviceList = GsonUtil.jsonToList(listArray.toString(), AdviceListBean.class);
                adviceAllList.addAll(adviceList);
                setData(page, false, adviceList, rows);
            }
        });
    }

    private void deleteData(String id) {
        HttpSender sender = new HttpSender(this, baseUrl, Urls.ADVICE_DELETE);
        sender.requestPost("id", id, new HttpCallBack(sender) {
            @Override
            public void onError(String code, Call call, Exception e) {

            }

            @Override
            public void onResponse(String code, String response) {
                if (adviceList != null) {
                    adviceList.clear();
                }
                requestData(true);//删除成功后刷新UI
            }
        });
    }
}
